// pages/index/index.js
const app = getApp()
let util = require('../../utils/util.js')
Page({
  data: {
    time: '00:00',
    h:'',
    m:'',
    s:'',
    gogo:false,
    gooffwork:true
  },
  onLoad: function (options) {
    let nowDay = new Date().getDate()
    if(wx.getStorageSync('goDay') == nowDay){
      this.setData({gooffwork:false})
      if(wx.getStorageSync('timeStorage')){
        let timeStorage = wx.getStorageSync('timeStorage');
        wx.setStorageSync('goDay',nowDay);
        this.getTime(timeStorage)
        this.setData({time:timeStorage});
      }
    } else{
      wx.setStorageSync('goDay',nowDay);
    }  
    
  },
  startWork(){
    this.setData({gooffwork:false})
  },
  bindTimeChange: function (e) {
    let time = e.detail.value;
    wx.setStorageSync('timeStorage',time);
    this.getTime(time)
    this.setData({time});
  },
  getTime(nowTimer) {
    let that = this;
    clearInterval(that.timer)
    that.timer = setInterval(function () {
      // 当前时间
      let now = new Date();
      // 下班时间
      let timeY = new Date().getFullYear();
      let timeM = new Date().getMonth() + 1;
      let timeD = new Date().getDate();
      let timeAll = timeY + '/' + timeM + '/' + timeD + ',' + nowTimer + ':00';
      let endtime = new Date(timeAll)
      let lefttime = parseInt((endtime.getTime() - now.getTime()) / 1000);
      if (lefttime < 0) {
        that.setData({gogo:true})
        clearInterval(that.timer)
        return
      }
      let h = parseInt(lefttime / (60 * 60) % 24);
      let m = parseInt(lefttime / 60 % 60);
      let s = parseInt(lefttime % 60);
      h = h > 9 ? h : '0' + h;
      m = m > 9 ? m : '0' + m;
      s = s > 9 ? s : '0' + s;
      that.setData({h,m,s})
    }, 1000)
  },
  close(){
    this.setData({gogo:false,gooffwork:true})
  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})